package com.example.demo.web;

import com.example.demo.annotation.SysLog;
import com.example.demo.bean.Dept;
import com.example.demo.bean.User;
import com.example.demo.bean.group.UserUpdate;
import com.example.demo.common.ResponseResult;
import com.example.demo.common.ResponseResultFactory;
import com.example.demo.event.guava.CashBackListener;
import com.example.demo.event.guava.EventBusService;
import com.example.demo.event.guava.IntegralEventListener;
import com.example.demo.event.guava.UserRegisterEvent;
import com.example.demo.utils.ValidatorUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by lyc on 2019/5/31.
 * 验证测试
 * 更多参考
 * https://blog.csdn.net/qq_27384769/article/details/79409057
 */
@RestController
@Validated
public class UserAction {


    @Autowired
    private EventBusService eventBusService;
    @Autowired
    private IntegralEventListener eventListener;

    @Autowired
    private CashBackListener cashBackListener;

   /* @Autowired
    private AmqpTemplate rabbitTemplate;*/

    /**
     * 常规校验
     * @param user
     * @return
     */
    @PostMapping("user/add")
    @SysLog(value = "添加用户")
    public ResponseResult addUser(@Valid User user){

        //发布一个事件
        //SpringUtils.publishEvent(new UserRegisterEvent(user));
        UserRegisterEvent event = new UserRegisterEvent("注册了");
        //注册一个监听
        eventBusService.register(eventListener);
        eventBusService.register(cashBackListener);
        //发布一个事件
        eventBusService.post(event);
        //rabbitTemplate.convertAndSend("register","注册了");

        return ResponseResultFactory.success("添加成功");
    }

    /**
     * 分组校验
     * @param user
     * @return
     */
    @PostMapping("user/update")
    public ResponseResult updateUser(@Validated(value = UserUpdate.class) User user){

        return ResponseResultFactory.success("添加成功");
    }


    /**
     * 工具校验
     * @param user
     * @return
     */
    @GetMapping("findUserById")
    public ResponseResult findUserById(User user){
        ValidatorUtils.validateEntity(user,null);
        return ResponseResultFactory.success("添加成功");
    }

    /**
     * 单个参数
     * @param id
     * @return
     */
    @GetMapping("findById")
    public ResponseResult findfindUserById(@NotNull(message = "id不能为空") Integer id){

        return ResponseResultFactory.success("添加成功");
    }


    /**
     * 嵌套验证
     * @return
     */
    @PostMapping("add/dept")
    public ResponseResult addDept(){

        Dept dept = new Dept();

        dept.setCreateDate("2019-12-12 12:16:16");

        User user1 = new User();

        User user2 = new User();
        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        dept.setUsers(users);

        ValidatorUtils.validateEntity(dept);
        return ResponseResultFactory.success("添加成功");
    }


    /**
     * 测试自定义 校验 与参数解析
     * @param user
     * @return
     */
    @PostMapping("edit/users")
    public ResponseResult editDate(@Validated User user){
        return ResponseResultFactory.success("添加用户",user);
    }


}
